home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / SharedQueue.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  5KB  |  183 lines

  1. /* SharedQueue.c -- implementation of shared queues
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     December, 1985
  21.  
  22. Function:
  23.  
  24. Class SharedQueue provides a queue that can be used for communication
  25. between two separate processes.  Semaphores are used to block a process
  26. if next() is called and there are no objects in the queue, or if
  27. nextPut() is called and there is no space in the queue.
  28.  
  29. $Log:    SharedQueue.c,v $
  30.  * Revision 3.0  90/05/20  00:21:20  kgorlen
  31.  * Release for 1st edition.
  32.  * 
  33. */
  34.  
  35. #include "SharedQueue.h"
  36. #include "nihclIO.h"
  37.  
  38. #define    THIS    SharedQueue
  39. #define    BASE    Object
  40. #define BASE_CLASSES BASE::desc()
  41. #define MEMBER_CLASSES ArrayOb::desc(),Semaphore::desc(),Semaphore::desc()
  42. #define VIRTUAL_BASE_CLASSES Object::desc()
  43.  
  44. DEFINE_CLASS(SharedQueue,2,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/SharedQueue.c,v 3.0 90/05/20 00:21:20 kgorlen Rel $",NULL,NULL);
  45.  
  46. SharedQueue::SharedQueue(unsigned queueSize)
  47. :
  48.     queue(queueSize),
  49.     valueAvailable(0),
  50.     spaceAvailable(queueSize)
  51. {
  52.     readPosition = writePosition = 0;
  53. }
  54.  
  55. SharedQueue::SharedQueue(const SharedQueue& q)
  56. :
  57.     queue(q.queue),
  58.     valueAvailable(q.valueAvailable),
  59.     spaceAvailable(q.spaceAvailable)
  60. {
  61.     readPosition = q.readPosition;
  62.     writePosition = q.writePosition;
  63. }
  64.  
  65. Object* SharedQueue::next()
  66. {
  67.     valueAvailable.wait();
  68.     Object* ob = queue[readPosition];
  69.     queue[readPosition++] = nil;
  70.     if (readPosition >= queue.capacity()) readPosition = 0;
  71.     spaceAvailable.signal();
  72.     return ob;
  73. }
  74.  
  75. Object* SharedQueue::nextPut(Object& ob)
  76. {
  77.     spaceAvailable.wait();
  78.     queue[writePosition++] = &ob;
  79.     if (writePosition >= queue.capacity()) writePosition = 0;
  80.     valueAvailable.signal();
  81.     return &ob;
  82. }
  83.  
  84. unsigned SharedQueue::capacity() const    { return queue.capacity(); }
  85.  
  86. bool SharedQueue::isEmpty() const    { return valueAvailable.value() <= 0; }
  87.  
  88. unsigned SharedQueue::size() const    { return MAX(valueAvailable.value(),0); }
  89.  
  90. Object* SharedQueue::copy() const    { return deepCopy(); }
  91.  
  92. void SharedQueue::deepenShallowCopy()
  93. {
  94.     queue.deepenShallowCopy();
  95.     valueAvailable.deepenShallowCopy();
  96.     spaceAvailable.deepenShallowCopy();
  97. }
  98.  
  99. void SharedQueue::dumpOn(ostream& strm) const
  100. {
  101.     strm << className() << "[\n";
  102.     strm << "valueAvailable ";  valueAvailable.dumpOn(strm);
  103.     strm << "spaceAvailable ";  spaceAvailable.dumpOn(strm);
  104.     strm << "queue:\n";
  105.     int i = readPosition;
  106.     for (int n = valueAvailable.value(); n>0; n--) {
  107.         queue[i++]->dumpOn(strm);
  108.         if (i == queue.capacity()) i = 0;
  109.     }
  110.     strm << "]\n";
  111. }
  112.  
  113. unsigned SharedQueue::hash() const    { return (unsigned)this; }
  114.  
  115. bool SharedQueue::isEqual(const Object& ob) const   { return isSame(ob); }
  116.  
  117. void SharedQueue::printOn(ostream& strm) const
  118. {
  119.     int i = readPosition;
  120.     for (int n = valueAvailable.value(); n>0; n--) {
  121.         if (i != readPosition) strm << '\n';
  122.         queue[i++]->printOn(strm);
  123.         if (i == queue.capacity()) i = 0;
  124.     }
  125. }
  126.  
  127. static unsigned sharedqueue_capacity;
  128.  
  129. SharedQueue::SharedQueue(OIOin& strm)
  130.     : BASE(strm),
  131.     queue((strm >> sharedqueue_capacity, sharedqueue_capacity)),
  132.     valueAvailable(strm),
  133.     spaceAvailable(strm)
  134. {
  135.     readPosition = 0;
  136.     writePosition = valueAvailable.value();
  137.     for (register int i =0; i<writePosition; i++) queue[i] = Object::readFrom(strm);
  138. }
  139.  
  140. void SharedQueue::storer(OIOout& strm) const
  141. {
  142.     BASE::storer(strm);
  143.     strm << queue.capacity();
  144.     valueAvailable.storeMemberOn(strm);
  145.     spaceAvailable.storeMemberOn(strm);
  146.     register int i = readPosition;
  147.     while (i != writePosition) {
  148.         queue[i++]->storeOn(strm);
  149.         if (i == queue.capacity()) i = 0;
  150.     }
  151. }
  152.  
  153. SharedQueue::SharedQueue(OIOifd& fd)
  154.     : BASE(fd),
  155.     queue((fd >> sharedqueue_capacity, sharedqueue_capacity)),
  156.     valueAvailable(fd),
  157.     spaceAvailable(fd)
  158. {
  159.     readPosition = 0;
  160.     writePosition = valueAvailable.value();
  161.     for (register int i=0; i < writePosition; i++)
  162.       queue[i] = Object::readFrom(fd);
  163. }
  164.  
  165. void SharedQueue::storer(OIOofd& fd) const
  166. {
  167.     BASE::storer(fd);
  168.     fd << queue.capacity();
  169.     valueAvailable.storeMemberOn(fd);
  170.     spaceAvailable.storeMemberOn(fd);
  171.     register int i = readPosition;
  172.     while (i != writePosition) {
  173.       queue[i++]->storeOn(fd);
  174.       if (i == queue.capacity()) i = 0;
  175.       };
  176. }
  177.  
  178. int SharedQueue::compare(const Object&) const
  179. {
  180.     shouldNotImplement("compare");
  181.     return 0;
  182. }
  183.